package controle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import modelo.dao.ContatoDAO;
import modelo.dao.UsuarioDAO;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

public class AdicionarUsuarioAction extends Action{
	
	@Override
	public ActionForward execute(
				ActionMapping mapping, 
				ActionForm form,
				HttpServletRequest request, 
				HttpServletResponse response)
			throws Exception {

		AdicionarUsuarioBean bean = (AdicionarUsuarioBean)form;
		UsuarioDAO dao = new UsuarioDAO();
		ActionMessages msg = new ActionMessages();
		
		if(dao.logar(bean.getNome(), bean.getSenha() )){
			msg.add("nome", new ActionMessage("usuario.nomeigual"));
			saveErrors(request, msg);
		}
		else if(dao.adicionarUsuario(bean.getNome(), bean.getSenha() )){
			msg.add("nome", new ActionMessage("usuario.cadastrado"));
			saveMessages(request, msg);
			return mapping.findForward("sucesso");
		}
		
		return mapping.findForward("erro");
	}

}
